//
//  arcView.m
//  圆弧进度条显示
//
//  Created by yhj on 15/12/10.
//  Copyright © 2015年 QQ:1787354782. All rights reserved.
//

#import "arcView.h"

@implementation arcView

//+(arcView *)shareInstance{
//
//    static arcView *arcview = nil;
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        arcview = [[arcView alloc]init];
//        
//    });
//    return arcview;
//}

-(void)drawRect:(CGRect)rect
{
    // 仪表盘底部
    drawHu1();
    [self drawHu3];
    // 仪表盘进度
    [self drawHu2];
    
    [self drawHu4];
}

-(void)drawHu2
{
    //1.获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    //1.1 设置线条的宽度
    CGContextSetLineWidth(ctx, 20);
    //1.2 设置线条的起始点样式
    CGContextSetLineCap(ctx,kCGLineCapButt);
    //1.3  虚实切换
    CGFloat length[] = {2,2};
    CGContextSetLineDash(ctx, 0, length, 2);
    //1.4 设置颜色
    [[[UIColor alloc]initWithRed:80/255.0 green:218/255.0 blue:84/255.0 alpha:1] set];
    
    //2.设置路径
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(numberChange:) name:@"number" object:nil];
    
    if (_num > 9) {
        [[[UIColor alloc]initWithRed:252/255.0 green:194/255.0 blue:52/255.0 alpha:1] set];
    }
    if (_num > 15) {
        [[[UIColor alloc]initWithRed:241/255.0 green:0/255.0 blue:6/255.0 alpha:1] set];
    }
    
    if (_num > 20) {
        _num = 20;
    }
    
    CGFloat end = 135*(M_PI/180) + 270*(M_PI/180)*_num/20;
    CGContextAddArc(ctx, (APPW-10*2-80)/2,(APPW-10*2-110)/2 + 70, 80, 135*(M_PI/180) , end, 0);
    
    //3.绘制
    CGContextStrokePath(ctx);
}

-(void)drawHu3
{
    //1.获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    //1.1 设置线条的宽度
    CGContextSetLineWidth(ctx, 20);
    //1.2 设置线条的起始点样式
    CGContextSetLineCap(ctx,kCGLineCapButt);
//    1.3  虚实切换
    CGFloat length[] = {2,2};//绘制，间隔
    CGContextSetLineDash(ctx, 0, length, 2);
    //1.4 设置颜色
   [[[UIColor alloc]initWithRed:225.0/255.0 green:225.0/255.0 blue:225.0/255.0 alpha:1] set];
    
    
    
       CGContextAddArc(ctx, (APPW-10*2-80)/2,(APPW-10*2-110)/2 + 70, 80, 135*(M_PI/180), 45*(M_PI/180),  0);
    
    //3.绘制
    CGContextStrokePath(ctx);
}
-(void)drawHu4
{
    //1.获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    //1.1 设置线条的宽度
    CGContextSetLineWidth(ctx, 50);
    //1.2 设置线条的起始点样式
    CGContextSetLineCap(ctx,kCGLineCapRound);
    //    1.3  虚实切换
//    CGFloat length[] = {2,2};//绘制，间隔
//    CGContextSetLineDash(ctx, 0, length, 2);
    //1.4 设置颜色
    [[[UIColor alloc]initWithRed:62/255.0 green:118/255.0 blue:218/255.0 alpha:1] set];
    
    CGContextAddArc(ctx, (APPW-10*2-80)/2,(APPW-10*2-110)/2 + 70, APPW-100, 0*(M_PI/180), 360*(M_PI/180),  0);
    
    //3.绘制
    CGContextStrokePath(ctx);
}


-(void)numberChange:(NSNotification *)text
{
    _num =[text.userInfo[@"num"] floatValue];
    [self setNeedsDisplay];
}

-(void)setNum:(double)num
{
    _num = num;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self =[super initWithFrame:frame];
    if (self) {
        
//        UILabel *lab2 = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 120, 30)];
//        lab2.text =@"单位：步（step）";
//        lab2.font = [UIFont systemFontOfSize:13];
//        lab2.backgroundColor = [UIColor redColor];
        
        UILabel *speed = [[UILabel alloc]initWithFrame:CGRectMake((APPW-120)/2, (APPW-80)/2, 120, 30)];
        speed.center = CGPointMake(self.center.x - 50, (APPW-80)/2 + 30);
        speed.text = @"速度";
        speed.textAlignment = NSTextAlignmentCenter;
        speed.textColor = [UIColor whiteColor];
        
        _numLabel=[[UILabel alloc]initWithFrame:CGRectMake((APPW-120)/2, (APPW-80)/2+30, 120, 30)];
        _numLabel.center = CGPointMake(self.center.x- 50, (APPW-80)/2 + 60);
        _numLabel.textAlignment = NSTextAlignmentCenter;
        _numLabel.textColor=[UIColor blackColor];
        _numLabel.text = @"0m/s";
        _numLabel.font=[UIFont systemFontOfSize:24];
        _numLabel.textColor = [UIColor whiteColor];
        if (!_timer) {
            _timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(change) userInfo:nil repeats:YES];
        }
        UILabel *minSpeed = [[UILabel alloc]initWithFrame:CGRectMake((APPW-120)/2 - 70, (APPW-80)/2+110, 120, 30)];
        minSpeed.center = CGPointMake(self.center.x - 120, (APPW-80)/2+120);
        minSpeed.text = @"0";
        minSpeed.textAlignment = NSTextAlignmentCenter;
        minSpeed.font=[UIFont systemFontOfSize:12];
        minSpeed.textColor = [UIColor whiteColor];
        
        UILabel *maxSpeed = [[UILabel alloc]initWithFrame:CGRectMake((APPW-120)/2 + 70, (APPW-80)/2+110, 120, 30)];
        maxSpeed.center = CGPointMake(self.center.x+20, (APPW-80)/2+120);
        maxSpeed.text = @"20";
        maxSpeed.textAlignment = NSTextAlignmentCenter;
        maxSpeed.font=[UIFont systemFontOfSize:12];
        maxSpeed.textColor = [UIColor whiteColor];
        
        [self addSubview:_numLabel];
        [self addSubview:speed];
        [self addSubview:minSpeed];
        [self addSubview:maxSpeed];
        

    }
    return self;
}

-(void)change
{
//    _num +=1;
//    if (_num >10000) {
//        _num = 0;
//    }
    _numLabel.text = [NSString stringWithFormat:@"%.2fm/s",_num];
   
    _numLabel.font = [UIFont systemFontOfSize:24];
    NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:_numLabel.text,@"num", nil];
    
    // 创建通知
    NSNotification *noti = [NSNotification notificationWithName:@"number" object:nil userInfo:dic];
    
    // 发送通知
    [[NSNotificationCenter defaultCenter] postNotification:noti];
}

void drawHu1()
{
    //是主要的描画接口，支持基于路径的描画
    // 1 获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    // 1.1 设置线条的宽度
    CGContextSetLineWidth(ctx, 5);
    // 1.2 设置线条的起始点样式
    CGContextSetLineCap(ctx,kCGLineCapButt);
    
    // 1.3 虚实切换， 绘制10 跳过5
//    CGFloat length[] = {10,10};
//    CGContextSetLineDash(ctx, 0, length, 2);

    
    // 1.4 设置颜色
//    [[[UIColor alloc]initWithRed:0.0/255.0 green:201.0/255.0 blue:87.0/255.0 alpha:1] set];
    [[[UIColor alloc]initWithRed:25/255.0 green:159/255.0 blue:204/255.0 alpha:0.7]set];
    // 2 设置路径
    CGContextAddArc(ctx,(APPW-10*2-80)/2,(APPW-10*2-110)/2 + 70,95,45*(M_PI/180),135*(M_PI/180),1);
    
    // 3 绘制
    CGContextStrokePath(ctx);
}

@end
